package com.woniuxy.microloanserver.dao;

import com.woniuxy.microloanserver.bean.UserLoanInterestRateBean;
import com.woniuxy.microloanserver.bean.UserLoanQuotaInfo;
import org.apache.ibatis.annotations.*;

/**
 * Description: 用户借贷相关信息数据访问接口（额度、利率）
 * @Auther: HanLei
 * @Date: 2021/4/15:10:05
 */
@Mapper
public interface LoanQuotaDao {

    /*
     * Description :   根据用户id查询用户借贷额度及利率信息
     * ChangeLog : 1. 创建 (2021/4/15 9:41 );
     * @param userId    用户id
     * @return UserLoanQuotaInfo 用户借贷额度信息对象
     */
    @Results(id = "loanQuotaWithUser",
            value = {
                @Result(property = "userId", column = "user_id", id = true),
                @Result(property = "totalQuota", column = "total_quota"),
                @Result(property = "usedQuota", column = "used_quota"),
                @Result(property = "quotaStatus", column = "quota_status"),
                @Result(property = "repaymentTime", column = "repayment_time"),
                @Result(property = "isDel", column = "is_del"),
                @Result(property = "userLoanInterestRate", column = "user_id",
                        javaType = UserLoanInterestRateBean.class,
                        one = @One(select = "queryRateByUserId")
                )
            }
    )
    @Select("SELECT user_id, total_quota, used_quota, quota_status, repayment_time, is_del " +
            "FROM loan_quota " +
            "WHERE user_id = #{userId} AND is_del = 0")
    UserLoanQuotaInfo qureyQuotaByUserId(@Param("userId") Integer userId);

    /*
     * Description :   新增用户贷款额度信息
     * ChangeLog : 1. 创建 (2021/4/15 10:38 );
     * @param userLoanQuotaInfo 用户贷款额度信息对象
     * @return int  影响行数
     */
    @Insert("INSERT INTO loan_quota (user_id, total_quota) " +
            "VALUES (#{userId}, #{totalQuota})")
    int addQuota(UserLoanQuotaInfo userLoanQuotaInfo);

    /*
     * Description :   更新用户贷款额度信息
     * totalQuota（总额度）、usedQuota（已使用额度）、quotaStatus（额度状态）、repaymentTime（还款时间）、isDel（删除状态）
     * ChangeLog : 1. 创建 (2021/4/15 14:29 );
     * @param userLoanQuotaInfo    用户贷款额度信息对象
     * @param newQuota  用户贷款新总额度
     * @return int  影响行数
     */
    int update(UserLoanQuotaInfo userLoanQuotaInfo);


    /*
     * Description :   根据用户id查询用户借贷利率信息
     * ChangeLog : 1. 创建 (2021/4/15 9:41 );
     * @param userId    用户id
     * @return UserLoanInterestRateBean 用户借贷利率对象
     */
    @Select("SELECT user_id userId, daily, three, six, nine, twelve " +
            "FROM loan_interest_rate " +
            "WHERE user_id = #{userId} AND is_del = 0")
    UserLoanInterestRateBean queryRateByUserId(@Param("userId") Integer userId);

    /*
     * Description :   新增用户借贷利率信息
     * ChangeLog : 1. 创建 (2021/4/15 9:53 );
     * @param userLoanInterestRateBean  用户贷款利率对象
     * @return int  影响行数
     */
    @Insert("INSERT INTO loan_interest_rate(user_id, daily, three, six, nine, twelve) " +
            "VALUES (#{userId}, #{daily}, #{three}, #{six}, #{nine}, #{twelve})")
    int addUserRate(UserLoanInterestRateBean userLoanInterestRateBean);

    /*
     * Description :   更新用户贷款利率
     * ChangeLog : 1. 创建 (2021/4/15 17:16 );
     * @param null
     * @return null
     */
    @Update("UPDATE loan_interest_rate " +
            "SET daily = #{daily}, three = #{three}, six = #{six}, nine = #{nine}, twelve = #{twelve} " +
            "WHERE user_id = #{userId}")
    int updateUserRate(UserLoanInterestRateBean userLoanInterestRateBean);
}
